package com.education.ai.controller;

import com.education.ai.entity.Student;
import com.education.ai.entity.StudentAnalysis;
import com.education.ai.repository.StudentAnalysisRepository;
import com.education.ai.repository.StudentRepository;
import com.education.ai.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 学生管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;
    
    @Autowired
    private StudentAnalysisRepository studentAnalysisRepository;

    /**
     * 获取所有学生列表
     */
    @GetMapping
    public ResponseEntity<List<Student>> getAllStudents() {
        log.info("获取所有学生信息");
        return ResponseEntity.ok(studentService.findAll());
    }

    /**
     * 根据学生ID获取学生信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
        log.info("获取学生信息，ID: {}", id);
        return ResponseEntity.ok(studentService.findById(id));
    }

    /**
     * 创建新学生
     */
    @PostMapping
    public ResponseEntity<Student> createStudent(@RequestBody Student student) {
        log.info("创建新学生: {}", student.getName());
        return ResponseEntity.ok(studentService.save(student));
    }

    /**
     * 更新学生信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Student> updateStudent(@PathVariable Long id, @RequestBody Student student) {
        log.info("更新学生信息，ID: {}", id);
        student.setId(id);
        return ResponseEntity.ok(studentService.save(student));
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
        log.info("删除学生，ID: {}", id);
        studentService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取学生的学情分析
     */
    @GetMapping("/{studentId}/analysis")
    public ResponseEntity<?> getStudentAnalysis(@PathVariable String studentId, @RequestParam(required = false) String subject) {
        log.info("获取学生学情分析，学生ID：{}，学科：{}", studentId, subject);
        
        if (subject != null && !subject.isEmpty()) {
            StudentAnalysis analysis = studentAnalysisRepository.findByStudentIdAndSubject(studentId, subject);
            if (analysis != null) {
                return ResponseEntity.ok(analysis);
            } else {
                Map<String, String> response = new HashMap<>();
                response.put("message", "未找到学生在该学科的学情分析");
                return ResponseEntity.status(404).body(response);
            }
        } else {
            List<StudentAnalysis> analysisList = studentAnalysisRepository.findByStudentId(studentId);
            if (!analysisList.isEmpty()) {
                return ResponseEntity.ok(analysisList);
            } else {
                Map<String, String> response = new HashMap<>();
                response.put("message", "未找到学生的学情分析");
                return ResponseEntity.status(404).body(response);
            }
        }
    }

    /**
     * 根据年级查询学生
     */
    @GetMapping("/grade/{grade}")
    public ResponseEntity<List<Student>> getStudentsByGrade(@PathVariable String grade) {
        log.info("获取年级学生列表: {}", grade);
        return ResponseEntity.ok(studentService.findByGrade(grade));
    }

    /**
     * 根据班级ID查询学生
     */
    @GetMapping("/class/{classId}")
    public ResponseEntity<List<Student>> getStudentsByClass(@PathVariable String classId) {
        log.info("获取班级学生列表: {}", classId);
        return ResponseEntity.ok(studentService.findByClassId(classId));
    }
} 